package cn.lg.soar.system.biz.modules.auth.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.OneToManyDTO;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.common.util.current.UserContext;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.ConditionEnum;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.common.enums.LogTypeEnum;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.biz.modules.auth.entity.User;
import cn.lg.soar.system.biz.modules.account.entity.UserTag;
import cn.lg.soar.system.biz.modules.account.service.IUserTagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Validated
@Tag(name = "用户标签")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/user-tag")
public class UserTagController extends BaseController<UserTag, IUserTagService> {

    @Override
    protected Serializable getId(UserTag entity) {
        return entity.getId();
    }

    private final static String SUBJECT = "用户标签";

    @Log(value = "新增" + SUBJECT, type = LogTypeEnum.CREATE)
    @IdempotentByParam("#p0")
    @RequestMapping(path = "create", method = RequestMethod.POST)
    public HttpResult<Serializable> create(@RequestBody UserTag data) {
        return createResult(data);
    }

    @Log(value = "修改" + SUBJECT, type = LogTypeEnum.UPDATE)
    @RequestMapping(path = "update", method = RequestMethod.POST)
    public HttpResult<Void> update(@RequestBody @Validated UserTag data) {
        return super.updateResult(data);
    }

    @Log(value = "删除" + SUBJECT, type = LogTypeEnum.REMOVE)
    @RequestMapping(path = "remove", method = RequestMethod.POST)
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        return super.removeResult(ids);
    }

    @Operation(summary = "详情")
    @GetMapping("detail")
    public HttpResult<UserTag> detail(@Id Long id){
        return HttpResult.data(service.getById(id));
    }

    @RequestMapping(path = "query", method = RequestMethod.GET)
    public HttpResult<PageResult<UserTag>> query(PageQuery pageQuery, QueryParams params){
        params.addFilter(UserTag::getUserId, ConditionEnum.in, UserContext.getUserId(), 0L);
        return queryResult(pageQuery, params);
    }

    @Operation(summary = "标签用户")
    @GetMapping("users")
    public HttpResult<List<User>> users(@Id Long tagId){
        return HttpResult.data(service.users(tagId));
    }

    @Operation(summary = "用户标签")
    @GetMapping("tags")
    public HttpResult<List<UserTag>> tags(@Id Long userId){
        return HttpResult.data(service.tags(userId));
    }

    @Operation(summary = "查询标签用户")
    @GetMapping(value = "query-user")
    public HttpResult<PageResult<User>> queryUser(@RequestParam(name = "tagId") Long tagId, PageQuery query, User user){
        return HttpResult.data(
                service.queryUser(tagId, query, user)
        );
    }

    @Log(value = "添加标签用户", type = LogTypeEnum.CREATE)
    @Operation(summary = "添加标签用户")
    @PostMapping(value = "add-user")
    public HttpResult<Void> addUser(@RequestBody OneToManyDTO<Long, Long> dto){
        service.addUser(dto);
        return HttpResult.success();
    }

    @Log(value = "删除标签用户", type = LogTypeEnum.REMOVE)
    @Operation(summary = "删除标签用户")
    @PostMapping(value = "remove-user")
    public HttpResult<Void> removeUser(@RequestParam("tagId") @Id Long tagId, @RequestBody List<Long> userIds){
        service.removeUser(tagId, userIds);
        return HttpResult.success();
    }

}
